package com.aqa.concurrent;

import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * 测试通过不同的方式，删除集合元素：索引号、迭代器、For-Each、并行迭代器
 */
public class IteratorTest {

    ArrayList<String> array;

    @Before
    public void before() {
        array = new ArrayList<String>();
        array.add("cc");
        array.add("aa");
        array.add("bb");
        array.add("aa");
    }

    @Test
    public void reomveByIdx() {
        for (int i = 0; i < array.size(); i++) {
            if ("aa".equals(array.get(i))) {
                array.remove(i);
            }
        }
        System.out.println(array.size());
    }

    @Test
    public void reomveForEach() {
        array.forEach(item -> {
            if ("aa".equals(item)) {
                array.remove(item);
            }
        });
        System.out.println(array.size());
    }

    @Test
    public void reomveByArrayInIterator() {
        Iterator<String> it = array.iterator();
        while (it.hasNext()) {
            String item = it.next();
            if ("aa".equals(item)) {
                array.remove(item);
            }
        }
        System.out.println(array.size());
    }

    @Test
    public void reomveByIterator() {
        Iterator<String> it = array.iterator();
        while (it.hasNext()) {
            String item = it.next();
            if ("aa".equals(item)) {
                it.remove();
            }
        }
        System.out.println(array.size());
    }
}